package com.lanou.pianke.article.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.pianke.article.entity.Article;
import com.lanou.pianke.article.entity.ArticleComment;
import com.lanou.pianke.article.entity.ArticleInfo;
import com.lanou.pianke.article.service.ArticleService;
import com.lanou.pianke.common.utils.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @GetMapping("/show")
    public PageInfo<Article> showArticle(Integer pageNum, Integer pageSize, Integer categoryId){

        List<String> contentIds = articleService.selectArticleId();
        for (int i = 0; i < contentIds.size(); i++) {
            String contentId = contentIds.get(i);
            Integer commentNumber = articleService.selectCommentNumber(contentId);
              articleService.updateArticleComment(contentId, commentNumber);

            }

        PageHelper.startPage(pageNum,pageSize);
        List<Article> articles = articleService.selectArticle(categoryId);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        System.out.println(pageInfo.getPages());
        return pageInfo;

    }

    @GetMapping("/info")
    public ArticleInfo showArticleInfo(String contentId){
        return articleService.selectArticleInfo(contentId);
    }

    @GetMapping("/comments")
    public List<ArticleComment> showArticleComments(String contentId){
        return articleService.selectCommentInfo(contentId);
    }

    @PostMapping("/publish")
    public void publishArticle(Integer acid,String images,Long uid,String title,String allContent){
        String contentId = RandomString.getRandomString();
        long addtime = new Date().getTime();
        String text = allContent.substring(0,30);
        Article article = new Article(acid,contentId,images,uid,title,text,allContent,addtime);
        articleService.insertArticle(article);
    }

    @PostMapping("/publishComment")
    public void publishComment(String contentId,String content,Long uid,Long autherId){

        String commentId = RandomString.getRandomString();
        long addTime = new Date().getTime();
        ArticleComment articleComment = new ArticleComment(commentId,contentId,uid,autherId,addTime,content);
        articleService.insertArticleComment(articleComment);


    }

    //查看所有文章 by李洋
    @GetMapping("/showArticles")
    public PageInfo<Article> showArticles(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "7") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articles = articleService.showAll();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        System.out.println(pageInfo.getPages());
        return pageInfo;
    }

    @GetMapping("/addRedis")
    public void addArtcileRedis(){
        articleService.addArtcileRedis();
    }

    @GetMapping("/hotArtciles")
    public List<Article> showHotArtciles(){
       List<Article> articles = articleService.showHotArticle();
       return articles;
    }



}
